home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / quicktime / importers and exporters / decompressionandscaling / compression & scaling.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  4.8 KB  |  193 lines

  1. /*
  2.     File:        Compression & Scaling.c
  3.  
  4.     Contains:    This example shows how to convert a    version 2 PICT to a compressed QuickTime data buffer.
  5.                 Once the PICT is compressed, it is then decompressed to the window at one quarter 
  6.                 its original size.
  7.  
  8.     Written by: Edgar Lee    
  9.  
  10.     Copyright:    Copyright © 1991-1999 by Apple Computer, Inc., All Rights Reserved.
  11.  
  12.                 You may incorporate this Apple sample source code into your program(s) without
  13.                 restriction. This Apple sample source code has been provided "AS IS" and the
  14.                 responsibility for its operation is yours. You are not permitted to redistribute
  15.                 this Apple sample source code as "Apple sample source code" after having made
  16.                 changes. If you're going to re-distribute the source, we require that you make
  17.                 it clear in the source that the code was descended from Apple sample source
  18.                 code, but that you've made changes.
  19.  
  20.     Change History (most recent first):
  21.                 7/28/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  22.                 12/4/94        khs                changed the format of the file to the new look and feel
  23.  
  24. */
  25.  
  26.  
  27. // INCLUDES
  28. #include    <GestaltEqu.h>
  29. #include    <ToolUtils.h>
  30. #include    <SegLoad.h>
  31. #include    <Fonts.h>
  32.  
  33. #include     <ImageCompression.h>
  34. #include     <Movies.h>
  35.  
  36. /* Constant Declarations */
  37.  
  38. #define WLEFT( x )    (((qd.screenBits.bounds.right - qd.screenBits.bounds.left) - x) / 2)
  39. #define WTOP( x )    (((qd.screenBits.bounds.bottom - qd.screenBits.bounds.top) - x) / 2)
  40. #define PICTID 128
  41.  
  42. // GLOBALS
  43. WindowPtr gWindow;
  44.  
  45.  
  46. // FUNCTION PROTOTYPES
  47. void InitMac();
  48. void CheckForQuickTime();
  49. void DoScaleTest();
  50. void CreateWindow();
  51.  
  52.  
  53. // MAIN
  54. void main(void)
  55. {
  56.     InitMac();
  57.     CheckForQuickTime();
  58.  
  59.     CreateWindow();
  60.     DoScaleTest();
  61.  
  62.     while (!Button())
  63.         ;
  64. }
  65.  
  66.  
  67. void InitMac()
  68. {
  69.     InitGraf(&qd.thePort);
  70.     InitFonts();
  71.     InitWindows();
  72.     InitMenus();
  73.     TEInit();
  74.     InitDialogs(nil);
  75.     InitCursor();
  76.     FlushEvents(0, everyEvent);
  77. }
  78.  
  79.  
  80. void CheckForQuickTime()
  81. {
  82.     long version;
  83.  
  84.     if (Gestalt(gestaltQuickTime, &version) != noErr)
  85.     {
  86.         ParamText("\pQuickTime not installed.  Please install, then try again.", "\p", "\p", "\p");
  87.         Alert(128, nil);
  88.         ExitToShell();
  89.     }
  90. }
  91.  
  92.  
  93. void CreateWindow()
  94. {
  95.     Rect bounds =
  96.     {
  97.         0,  0, 0, 0
  98.     };
  99.  
  100.     gWindow = NewCWindow(0L, &bounds, "\pDecompression & Scaling", 
  101.                             false, documentProc, (WindowPtr) - 1L, false, 0L);
  102.  
  103.     SetPort(gWindow);
  104. }
  105.  
  106.  
  107. void DoScaleTest()
  108. {
  109.     int i;
  110.     GWorldPtr srcGWorld;
  111.     PixMapHandle srcPixMap;
  112.     PicHandle pict;
  113.     Rect pictBounds;
  114.     Rect finalBounds;
  115.     CGrafPtr savedPort;
  116.     GDHandle savedDevice;
  117.     short scaleRatio = 4;
  118.  
  119.     ImageDescriptionHandle desc;
  120.     Ptr imageData;
  121.     long size;
  122.  
  123.     // Load the picture and define the source and dest rects.
  124.     pict = GetPicture(PICTID);
  125.  
  126.     pictBounds = (**pict).picFrame;
  127.     OffsetRect(&pictBounds, -pictBounds.left, -pictBounds.top);
  128.  
  129.     finalBounds = pictBounds;
  130.     finalBounds.right = finalBounds.right / scaleRatio;
  131.     finalBounds.bottom = finalBounds.bottom / scaleRatio;
  132.  
  133.     SizeWindow(gWindow, pictBounds.right + finalBounds.right, pictBounds.bottom, false);
  134.     MoveWindow(gWindow, WLEFT(pictBounds.right + finalBounds.right), 
  135.                         WTOP(pictBounds.bottom), false);
  136.     ShowWindow(gWindow);
  137.  
  138.     /********************************************************/
  139.     /* Create a temporary offscreen used to store the pict. */
  140.     /********************************************************/
  141.  
  142.     if (NewGWorld(&srcGWorld, 8, &pictBounds, GetCTable(8), nil, 0) != noErr)
  143.         ExitToShell();
  144.  
  145.     srcPixMap = GetGWorldPixMap(srcGWorld);
  146.     LockPixels(srcPixMap);
  147.  
  148.     /********************************************************/
  149.     /* Draw the picture into the temporary gworld & window. */
  150.     /********************************************************/
  151.  
  152.     GetGWorld(&savedPort, &savedDevice);
  153.     SetGWorld(srcGWorld, nil);
  154.     DrawPicture(pict, &pictBounds);
  155.  
  156.     SetGWorld(savedPort, savedDevice);
  157.     DrawPicture(pict, &pictBounds);
  158.  
  159.     /*************************************************/
  160.     /* Now, compress the picture into a data buffer, */
  161.     /*   then dispose the temporary gworld.          */
  162.     /*************************************************/
  163.  
  164.     if (GetMaxCompressionSize(srcPixMap, &(**srcPixMap).bounds, 8, codecMaxQuality, 
  165.                                 'raw ', bestSpeedCodec, &size))
  166.         ExitToShell();
  167.  
  168.     imageData = NewPtr(size);
  169.     desc = (ImageDescriptionHandle)NewHandle((Size)1);
  170.  
  171.     if (CompressImage(srcPixMap, &(**srcPixMap).bounds, codecMaxQuality, 
  172.                         'raw ', desc, imageData))
  173.         ExitToShell();
  174.  
  175.     DisposeGWorld(srcGWorld);
  176.  
  177.     /**********************************************************/
  178.     /* Decompress the data buffer to the window with scaling. */
  179.     /**********************************************************/
  180.  
  181.     OffsetRect(&finalBounds, pictBounds.right, 0);
  182.  
  183.     for (i = 0; i < scaleRatio; i++)
  184.     {
  185.         DecompressImage(imageData, desc, (*(CWindowPtr)gWindow).portPixMap, 
  186.                             &pictBounds, &finalBounds, srcCopy + ditherCopy, nil);
  187.  
  188.         OffsetRect(&finalBounds, 0, finalBounds.bottom - finalBounds.top);
  189.     }
  190. }
  191.  
  192.  
  193.